package com.epam.training;

import java.security.Principal;
import java.util.Date;
import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.epam.training.domain.BookingItem;
import com.epam.training.domain.Car;
import com.epam.training.domain.Customer;
import com.epam.training.domain.Insurance;
import com.epam.training.formvalidate.BookingItemForm;
import com.epam.training.service.bookingitem.BookingItemService;
import com.epam.training.service.car.CarService;
import com.epam.training.service.insurance.InsuranceService;
import com.epam.training.service.user.UserService;

@Controller
@RequestMapping("/customer/newBooking/add")
public class AddCustomerBookingController {

	private final BookingItemService service;

	private final CarService carService;

	private final InsuranceService insuranceService;

	private final UserService userService;

	@Autowired
	public AddCustomerBookingController(BookingItemService service,
			CarService carService, InsuranceService insuranceService,
			UserService userService) {
		this.service = service;
		this.carService = carService;
		this.insuranceService = insuranceService;
		this.userService = userService;
	}

	@ModelAttribute("bookingItemForm")
	public BookingItemForm getBookingItemForm() {
		return new BookingItemForm();
	}

	public BookingItem createBookingItem(BookingItemForm form) {
		BookingItem item = new BookingItem();
		item.setDate(new Date());

		item.setDateOfIssue(form.getIssueDate());
		item.setDateOfReturn(form.getReturnDate());
		item.setPlaceOfIssue(form.getIssuePlace());
		item.setPlaceOfReturn(form.getReturnPlace());
		item.setStatus("disable");

		item.setCar(carService.getById(form.getCar()));
		item.setInsurance(insuranceService.getById(form.getInsurance()));

		return item;
	}

	@RequestMapping
	private String saveBooking(
			Model model,
			Principal principal,
			@ModelAttribute("bookingItemForm") @Valid BookingItemForm bookingItemForm,
			BindingResult result) {

		if (result.hasErrors()) {
			List<Car> carList = carService.getAllCar();
			List<Insurance> insuranceList = insuranceService.getAllInsurance();

			model.addAttribute("carList", carList);
			model.addAttribute("insuranceList", insuranceList);

			return "customerView/NewBooking";
		}

		Customer customer = userService
				.findCustomerByLogin(principal.getName());

		if (customer.getName() == null || customer.getSurname() == null
				|| customer.getSecondName() == null
				|| customer.getTelephone() == null
				|| customer.getDriveLicense() == null) {
			model.addAttribute("message", "page.error.notExistAccountFieald");

			return "redirect:/customer";
		}

		BookingItem bookingItem = createBookingItem(bookingItemForm);
		bookingItem.setCustomer(customer);

		service.save(bookingItem);

		model.addAttribute("message", "bookingitem.registered");

		return "redirect:/customer";
	}
}
